﻿
using BCGM.MedicalWisdom.IServices;
using BCGM.MedicalWisdom.Model.Models;
using BCGM.MedicalWisdom.Services.BASE;
using System.Threading.Tasks;
using System.Collections.Generic;
using BCGM.MedicalWisdom.Model.ViewModels.DoctorDTO;
using SqlSugar;
using BCGM.MedicalWisdom.Common.Helper;
using System.Linq;
using BCGM.MedicalWisdom.Repository.BASE;

namespace BCGM.MedicalWisdom.Services
{
    public class DoctorServices : BaseServices<Doctor>, IDoctorServices
    {
        private readonly IBaseRepository<Doctor> _dal;
        public DoctorServices(IBaseRepository<Doctor> dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
        }
        /// <summary>
        /// 医生登录
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public async Task<string> DoctorLogin(DoctorLoginDTO list)
        {
            var user = (await base.Query(a => a.Phone.Equals(list.Phone)&&a.Password.Equals(MD5Helper.MD5Encrypt16(list.PassWord)))).FirstOrDefault();
            if (user!=null)
            {
                return list.Phone;
            }
            return null;
        }

        /// <summary>
        /// 医生管理
        /// </summary>
        /// <param name="DoctorName"></param>
        /// <returns></returns>
        public async Task<List<DoctorDTOs>> GetBlogs(string DoctorName)
        {
            var sql = "SELECT * from Doctor WHERE Isdel =0 ";
            if (!string.IsNullOrEmpty(DoctorName))
            {
                sql += $" and Name like '%{DoctorName}%'";
            }
            SugarParameter[] parameters = null;
            var doctorlist = await _dal.QuerySql(sql,parameters);
            var DTOlist=new List<DoctorDTOs>();
            foreach (var d in doctorlist)
            {
                var dto = new DoctorDTOs
                {
                    Name = d.Name,
                    Photo = d.Photo,
                    AgeLimit = d.AgeLimit,
                    Hospital = d.Hospital,
                    Id = d.Id,
                    ConsultationTime = d.ConsultationTime,
                    IdentityCard = d.IdentityCard,
                    InquiryMoney = d.InquiryMoney,
                    Isdel = d.Isdel,
                    Phone = d.Phone,
                    occupationCard = d.occupationCard,
                    QualificationCard = d.QualificationCard,
                    UserId = d.UserId
                };
                DTOlist.Add(dto);
            }
            return DTOlist;
        }
    }
}